package events;


import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Observable;
import java.util.Observer;

import data.Coordinate;
import data.Dart;
import data.Game;
import data.GameLoop;
import data.Player;

/**
 * Classe che serve per implementare il lancio del dardo per non tenere
 * l'istanza del player che lancia i dardi si puo mettere un ID ID = indice del
 * player dentro il arraylist nel playzone.
 * 
 * @author TeamVulcan
 */
public class DardoAction extends Observable implements MouseListener,
		MouseMotionListener {

	private Dart dart;
	private Player player;
	private long lastTime = 0;

	public DardoAction(Player player) {
		super();
		this.player = player;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		long actualTime = System.currentTimeMillis();
		if ((actualTime - lastTime > player.getDart().getTimeDelay())
				&& (player.getHealthPoints() >= 0) && (player.canShoot())) {
			Coordinate c = new Coordinate(player.getPosition().getX()
					+ (Player.getSize() >> 1), player.getPosition().getY()
					+ (Player.getSize() >> 1));
			createDart(arg0.getX(), arg0.getY(), c);
			player.setDart(player.getDart().createClone());
		}
	}

	private void createDart(int xM, int yM, Coordinate c) {
		dart = player.getDart();
		dart.setOrigin(c);
		dart.setTeam(player.getTeam());
		dart.setTarget(new Coordinate(xM, yM));
		Game.getSingletonGame().getPlayZone().addDart(dart);
		GameLoop.addDart(dart);
		setChanged();
		notifyObservers("DardoSetTarget");
		clearChanged();
		dart.setDirection(getDirectionFromLocation(dart.getOrigin(),
				dart.getTarget()));
		lastTime = System.currentTimeMillis();
	}

	// Metodo per ottenere la direzione di sparo
	private float[] getDirectionFromLocation(Coordinate origin,
			Coordinate target) {
		float[] direction = new float[2];
		int x = origin.getX();
		int y = origin.getY();
		float d = (float) getDistance(target, x, y);
		x = (target.getX() - origin.getX());
		y = (target.getY() - origin.getY());
		direction[0] = x / d; // coseno del angolo di sparo
		direction[1] = y / d; // seno del angolo di sparo
		return direction;
	}

	private double getDistance(Coordinate target, int x, int y) {
		return Math.sqrt((x - target.getX()) * (x - target.getX())
				+ (y - target.getY()) * (y - target.getY()));
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// possibili miglioramenti per nuove abilita
		// (cambiare direzione del dardo quando e gia lanciato usando il drag).
	};

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}

	@Override
	public synchronized void addObserver(Observer o) {
		super.addObserver(o);
	}

}
